package com.jcfk.eam.controller.parent;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.*;

import com.jcfk.eam.domain.query.main.EamAssetInfoQuery;
import com.jcfk.eam.service.main.EamAssetInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import com.jcfk.eam.domain.query.main.EamAssetStatusChangeQuery;
import com.jcfk.eam.domain.dto.main.EamAssetStatusChangeDTO;
import com.jcfk.eam.domain.uploadExcel.main.EamAssetStatusChangeUploadExcel;
import com.alibaba.excel.EasyExcel;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import com.jcfk.eam.service.main.EamAssetStatusChangeService;
import com.jcfk.common.core.utils.ExcelUtils;
import com.jcfk.eam.domain.excel.main.EamAssetStatusChangeExcel;

import java.util.List;

/**
 * 资产状态变更记录
 *
 * @author yql
 * @email 522891237@gqq.com
 * @date 2024-07-25
 */
@Api(tags = {"资产状态变更记录管理"})
@RestController
@RequestMapping("eamassetstatuschange")
public class EamAssetStatusChangeController {
    @Autowired
    private EamAssetStatusChangeService eamAssetStatusChangeService;

    @Autowired
    private EamAssetInfoService eamAssetInfoService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamAssetStatusChangeQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询资产状态变更记录")
    @PostMapping("/queryByPage")
    public RetPage<EamAssetStatusChangeDTO> queryByPage(@RequestBody ReqPageParam<EamAssetStatusChangeQuery> requestParam) {
        //设置默认排序
        if(requestParam.getSort().getOrderItem()==null){
            ReqSortParam<EamAssetStatusChangeQuery> sortParam=new ReqSortParam();
            sortParam.setColumn("lastUpdateTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }

        DataPage<EamAssetStatusChangeDTO> page = new DataPage<EamAssetStatusChangeDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            EamAssetStatusChangeDTO.class);
        page = eamAssetStatusChangeService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     *  资产状态变更记录
     * 根据id 查询详情
     */
    @ApiOperation("查询资产状态变更记录")
    @GetMapping("/get/{id}")
    public RetResult<EamAssetStatusChangeDTO> get(@PathVariable("id") String id) {
            EamAssetStatusChangeDTO item = eamAssetStatusChangeService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理资产状态变更记录
     */
    @ApiOperation("保存资产状态变更记录")
    @PostMapping("/save")
    @RequestLogHeader(modular = "资产状态变更记录管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamAssetStatusChangeDTO eamAssetStatusChange) {
        // 数据校验
        RetResult ret = eamAssetStatusChangeService.isValid(eamAssetStatusChange);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        //修改资产台账状态，此处缺少对现有状态的校验。本应添加。但由于时间问题暂时未添加
        boolean isOk =  eamAssetInfoService.updateAssetStatus(eamAssetStatusChange.getAssetId(),eamAssetStatusChange.getStatusAfterChange());
        if(!isOk){
            return RetResult.fail("更新资产台账数据失败");
        }

        if (StrUtil.isEmpty(eamAssetStatusChange.getId())){
            // 新增
            eamAssetStatusChangeService.insert(eamAssetStatusChange);
        } else{
            // 更新
            eamAssetStatusChangeService.update(eamAssetStatusChange);
        }
        return RetResult.ok();

    }

    /**
     *  批量删除资产状态变更记录
     */
    @ApiOperation("批量删除资产状态变更记录")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "资产状态变更记录管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamAssetStatusChangeDTO eamAssetStatusChange) {
        return RetResult.ok(eamAssetStatusChangeService.removeBigBatch(eamAssetStatusChange.getIds()));
    }


    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody EamAssetStatusChangeQuery requestParam, HttpServletResponse response) throws Exception {
        List<EamAssetStatusChangeDTO> list = eamAssetStatusChangeService.getList(requestParam);
        ExcelUtils.exportExcelToTarget(response, null, list, EamAssetStatusChangeExcel.class);
    }

    /**
     * 导入资产状态变更记录
     */
    @PostMapping("upload")
    public RetResult<?> upload(MultipartFile file) throws IOException {
        // 读取excel
        List<EamAssetStatusChangeUploadExcel> items = EasyExcel.read(file.getInputStream(),EamAssetStatusChangeUploadExcel.class,null).autoCloseStream(true).sheet(0).headRowNumber(1).autoTrim(true).doReadSync();
        eamAssetStatusChangeService.insertBatch(items);
        return RetResult.ok();
    }

    /**
     * 下载模板
     */
    @PostMapping("exportTemp")
    @ApiOperation("下载模板")
    public void exportTemp(HttpServletResponse response) throws Exception {
        List<EamAssetStatusChangeUploadExcel> list = new ArrayList<>();
        list.add(new EamAssetStatusChangeUploadExcel());
        ExcelUtils.exportExcelToTarget(response, null, list, EamAssetStatusChangeUploadExcel.class);
    }


    /**
     *  删除资产状态变更记录
     */
    @ApiOperation("删除资产状态变更记录")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "资产状态变更记录管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamAssetStatusChangeService.deleteitem(id);
        return RetResult.ok();
    }
}
